home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Telnet 2.6.1d1 4⁄26⁄94 Folder / source / main / printing.c < prev    next >
Text File  |  1994-04-15  |  12KB  |  422 lines

  1. /*
  2. *    printing.c
  3. *
  4. *    This now contains the PrintPages() code from Roland Mansson, from Lund 
  5. *    University Computing Center.  This allows for printer redirection, and also
  6. *    printing page numbers (and other goodies).  Thanks for all of your work!
  7. *
  8. *
  9. *
  10. *****************************************************************
  11. *    NCSA Telnet for the Macintosh                                *
  12. *                                                                *
  13. *    National Center for Supercomputing Applications                *
  14. *    Software Development Group                                    *
  15. *    152 Computing Applications Building                            *
  16. *    605 E. Springfield Ave.                                        *
  17. *    Champaign, IL  61820                                        *
  18. *                                                                *
  19. *    Copyright (c) 1986-1992,                                    *
  20. *    Board of Trustees of the University of Illinois                *
  21. *****************************************************************
  22. *    Code to handle printing.
  23. */
  24.  
  25. #ifdef MPW
  26. #pragma segment Printing
  27. #endif
  28.  
  29. #include <Printing.h>
  30. #include    <stdio.h>
  31. #include <string.h>
  32.  
  33. #include "TelnetHeader.h"
  34. #include "wind.h"
  35. #include "rsmac.proto.h"    /* For RSGetSelText */
  36. #include "netevent.proto.h"
  37. #include "vgtek.proto.h"
  38. #include "rgmp.proto.h"
  39. #include "event.proto.h"
  40. #include "tekrgmac.proto.h"
  41.  
  42. #include "debug.h"
  43.  
  44. #include "vsdata.h"
  45. #include "vsinterf.proto.h"
  46.  
  47. extern Cursor *theCursors[];
  48. extern WindRec
  49.     *screens;            /* The screen array from Maclook */
  50. extern short scrn;
  51.  
  52. PROTO_UPP(printsleep, PrIdle);
  53. extern    void printsleep(void);
  54.  
  55. THPrint    gPrRecHandle = NULL;
  56.  
  57. #define PGRECT    (*PrRecHandle)->prInfo.rPage        /* Macro for making the pagerect more accessible */
  58. #define VMARGINS 4
  59. #define PAPRECT (*PrRecHandle)->rPaper /* Macro for making the paperrect more accessible */
  60. #define bDevCItoh 1 /* from Printing.h */
  61. #define ascLF 10
  62. #define ascFF 12
  63. #define ascCR 13
  64.  
  65. #include "printing.proto.h"
  66.  
  67. void    PrintingUnload(void) {}
  68.  
  69. /*    userabort - check to see if the user wants to abort */
  70. short userabort(void)
  71. {
  72.     EventRecord theEvent;
  73.  
  74.     while (GetNextEvent(24,&theEvent)) {
  75.         if ((theEvent.modifiers & cmdKey) &&
  76.             (theEvent.message & 0xff) =='.') 
  77.             return(-1);
  78.         }
  79.     return(0);
  80. }
  81.  
  82. /*    printsleep()    - Provide the network keep alive while we are in print
  83.  *                      mode.  Also set the abort flag if necessary. */
  84. SIMPLE_UPP(printsleep, PrIdle);
  85. void printsleep(void)
  86. {
  87.     short    CurrentResFile;
  88.     
  89.     CurrentResFile = CurResFile();        /* Save the current resource file just in case. */
  90.     Stask();                            /* act like the postman */
  91.     if (userabort())
  92.         (*gPrRecHandle)->prJob.fFromUsr=TRUE;
  93.     UseResFile(CurrentResFile);            /* Restore this in case we were bad and changed it. */
  94. }
  95.  
  96. /*    printGraph -    Print the graphics on the current window
  97.  *        vg - which graphics window to print */
  98.  
  99. void printGraph(short dnum)            /* Which drawing to print */
  100. {
  101.     short h,v;            /* used for centering (h=horiz. , v=vert.) */
  102.     short wh,wv;            /* Window horiz and vert */
  103.     TPrStatus
  104.         prStatus;        /* Printing status record */
  105.     Rect prRect;        /* the rectangle to print in */
  106.     TPPrPort prPort;    /* the printing port */
  107.     short j;                /* VG identifier for pass-through */
  108.     THPrint        PrRecHandle;    /* our print record handle */
  109.  
  110.     PrRecHandle = PrintSetupRecord();
  111.         
  112.     SetCursor(theCursors[normcurs]);
  113.  
  114.     if (PrJobDialog(PrRecHandle)) {            /* Cancel the print if FALSE */
  115.         prPort=PrOpenDoc(PrRecHandle,0L,0L);        /* Open Printer Port */
  116.             if (PrError() == 0)    {                /* If we can't, then die */
  117.                 PrOpenPage(prPort, 0L);                /* Open a page */
  118.  
  119.                 (*PrRecHandle)->prJob.pIdleProc = printsleepUPP;    /* our sleep/cancel*/
  120.  
  121.                 prRect=PGRECT;                        /* Get the page rectangle */
  122.                 h=prRect.right - prRect.left;    /* Get the width */
  123.                 v=prRect.bottom- prRect.top;    /* Get the height */
  124.                 if (3*h<4*v) {                    /* Center the little bugger */
  125.                     wh = h;
  126.                     wv = (3 * h)/ 4;
  127.                     }
  128.                 else {                            /* On the page rectangle */
  129.                     wv = v;
  130.                     wh = (4 * v)/ 3;
  131.                     }
  132.         
  133.                 prRect.top  = (v- wv) /2;
  134.                 prRect.left = (h- wh) /2;
  135.                 prRect.bottom = prRect.top + wv;
  136.                 prRect.right = prRect.left + wh;
  137.  
  138.                 j=VGnewwin(3,VGgetVS(dnum));        /* NCSA 2.5: fixed the print call */
  139.                 RGMPsize( &prRect );
  140.                 VGzcpy( dnum, j);                /* Love dat zm factr */
  141.                 VGredraw(dnum,j);                /* Copy the picture in i to j */
  142.                 VGclose(j);                        /* OK, we're done, give it to someone else */
  143.                 PrClosePage(prPort);            /* Page out.... */
  144.                 }
  145.  
  146.             PrCloseDoc(prPort);                    /* Done with the document */
  147.             putln("Doc is closed... the printing begins....");
  148.  
  149.         if (PrError()==0)                                                    /* BYU 2.4.20 - changed for HP DeskWriter*/
  150.             PrPicFile(PrRecHandle,0L,0L,0L,&prStatus);
  151.                                     /* Print the spooled file if applicable */
  152.     }        /* if PrJobDialog */
  153.     
  154.     DisposeHandle((Handle)PrRecHandle);
  155.     updateCursor(1);
  156. }
  157.  
  158.  
  159.  
  160.  
  161.  
  162. /* LU - this is the NEW PrintPages code.  This handles page numbering, and also
  163.         allows us to dump the redirected output to the printer.  Written by
  164.         Roland Mansson, Lund University Computing Center.  Thank HIM for this code... */
  165. /* Some patches had to be made to get it to compile under THINK C, and to work        */
  166. /* with some of the changes we have recently made.   - SMB                            */
  167.  
  168.  
  169.  
  170.  
  171. /*    printPages(prPort,charh,title, columns) -
  172.  *            Prints <charh> on the printer through port <prPort> with <title>
  173.  *            using <columns>.
  174.  */
  175. void printPages(TPPrPort prPort, THPrint PrRecHandle, Str255 Title, short columns, char **charh, short refNum, short useTitle, short theScreen)
  176. {
  177.     char    *charp, *start;
  178.     long    charlen,count=0L;
  179.     short        v, h, scount, lines, pgcount, pgtmplen, maxlines;
  180.     char    pagetemp[20];
  181.     FMetricRec info;
  182.     short        pFheight, pFwidth;
  183.     unsigned char buf[256];        /* to build a line in from the file */
  184.     unsigned char nextchar;        /* next unprocessed char in file */
  185.     short        rdy;
  186.     short    indent;                /* indent to give reasonable left marginal */
  187.     OSErr    sts;
  188.     long    dummyCount;
  189.     char    tmp[100];            /* only for debugging */
  190.     char    stupidarray[160];    /* used in menuseg for finding string widths */
  191.  
  192.     for (v=0; v<150; v++) stupidarray[v]='W';    /* Set up the width array */
  193.  
  194.     indent = ((*PrRecHandle)->prInfo.iHRes * 180)/254;    /* 1.8 centimeters left margin */
  195.     if (-PAPRECT.left > indent)
  196.         indent = 0;
  197.     else
  198.         indent += PAPRECT.left;
  199.  
  200.     if ( ((*PrRecHandle)->prStl.wDev>>8) == bDevCItoh ) {
  201.                     /* Think about this: put def. font in rsrc fork- STR255 */
  202.         TextFont(monaco);            /* gives monaco on ImageWriters … */
  203.         v=9;
  204.         }
  205.     else {                            /* NCSA: SB - use current font */
  206.         TextFont((*(screens[theScreen].wind)).txFont);        /* NCSA: SB */
  207.         TextFace((*(screens[theScreen].wind)).txFace);        /* NCSA: SB */
  208.         v = (*(screens[theScreen].wind)).txSize;            /* NCSA: SB */
  209.         }                                                    /* NCSA: SB */
  210.     
  211.     do {
  212.         TextSize(v);
  213.         FontMetrics( &info );
  214.         pFheight = FixRound( info.ascent + info.descent + info.leading);
  215.         pFwidth  = FixRound( info.widMax);
  216.         pFwidth  = CharWidth('W');            /* Re-assign to largest char */
  217.         v--;
  218.     } while ( TextWidth(stupidarray,0,columns+1) > (PGRECT.right - PGRECT.left - indent));
  219.     sprintf (tmp,"Fheight:%d, Fwidth:%d, TextSize:%d",pFheight,pFwidth,v+1); putln (tmp);
  220.  
  221.     if (charh!=NULL) {
  222.         HLock(charh);
  223.         start=charp=*charh;
  224.         charlen= GetHandleSize(charh);
  225.     } else {
  226.         if (sts=GetFPos(refNum, &charlen))
  227.             {
  228.             sprintf(tmp,"GetFPos: ERROR %d",(short)sts);
  229.             putln(tmp);
  230.             }
  231.         charlen-=3;                    /* skip last 3 chars, as they are part of ESC seq */
  232.         if (sts=SetFPos(refNum,(short) fsFromStart,0L))    /* rewind to beginning of file */
  233.             { sprintf(tmp,"SetFPos: ERROR %d",sts); putln(tmp); }
  234.         start = (char *)buf;                /* always (the array doesn't move) */
  235.         dummyCount=1;
  236.         if (sts=FSRead(refNum,&dummyCount,&nextchar))        /* get first char */
  237.             { sprintf(tmp,"FSRead: ERROR %d",sts); putln(tmp); }
  238.     }
  239.     
  240.     h=PGRECT.right - PGRECT.left - indent;    /* Get the width */
  241.     v=PGRECT.bottom- PGRECT.top;            /* Get the height */
  242.  
  243.     maxlines = v/pFheight-1;
  244.     pgcount = 0;
  245.     while (count<charlen) {
  246.         pgcount++;
  247.         lines = 1;
  248.         PrOpenPage(prPort, 0L);        /* Open the Printer Page */
  249.         if (sts=PrError()) { sprintf(tmp,"PrOpenPage: ERROR %d",sts); putln(tmp); }
  250.         (*PrRecHandle)->prJob.pIdleProc= printsleepUPP;            /* our netsleep */
  251.         sprintf (tmp,"printing page:%d",pgcount); putln(tmp);
  252.  
  253.         if (useTitle) {
  254.             MoveTo( indent, lines*pFheight);
  255.             DrawString("\pSession Name: ");            /* BYU LSC */
  256.     
  257.             DrawString( Title);            /* BYU LSC */
  258.             sprintf(pagetemp, "Page %d", pgcount);
  259.             pgtmplen=strlen(pagetemp);
  260.             MoveTo( PGRECT.right-(pgtmplen * pFwidth)-1, lines++*pFheight);
  261.             DrawText( pagetemp, 0, pgtmplen);
  262.             lines++;                    /* one blank line after title line */
  263.         }
  264.  
  265.         if (charh!=NULL) {                                    /* print from handle */
  266.             while (lines <= maxlines && count<charlen) {
  267.                 scount=0;
  268.                 while ((count<charlen) && (*charp++!='\r')) { count++; scount++; }
  269.                 MoveTo(indent,lines++*pFheight);
  270.                 if (scount>0)
  271.                     DrawText(start, 0, scount);
  272.                 count++;
  273.                 start=charp;
  274.             }
  275.         } else {                                            /* print from file */
  276.             while (lines <= maxlines && count<charlen) {
  277.                 rdy = 0;
  278.                 scount = 0;
  279.                 while ((count<charlen) && (!rdy)) {
  280.                     switch (nextchar) {
  281.                         case ascCR:
  282.                             rdy=1;
  283.                             break;
  284.                         case ascLF:
  285.                             rdy=1;
  286.                             break;
  287.                         case ascFF:
  288.                             rdy=1;
  289.                             break;
  290.                         default:
  291.                             buf[scount++]=nextchar;
  292.                             count++;
  293.                             dummyCount=1;
  294.                             if (sts=FSRead (refNum,&dummyCount,&nextchar))
  295.                                 { sprintf(tmp,"FSRead: ERROR %d",sts); putln(tmp); }
  296.                             break;
  297.                     }
  298.                 }
  299.                 MoveTo(indent,lines*pFheight);
  300.                 if (scount>0)
  301.                     DrawText(start, 0, scount);
  302.                 if (nextchar==ascLF)
  303.                     lines++;                        /* LF -> new line */
  304.                 if (nextchar==ascFF)
  305.                     lines = maxlines+1;                /* FF -> new page */
  306.                 dummyCount=1;
  307.                 if (sts=FSRead (refNum,&dummyCount,&nextchar))
  308.                     { sprintf(tmp,"FSRead: ERROR %d",sts); putln(tmp); }
  309.                 count++;
  310.             }
  311.         }
  312.         PrClosePage(prPort);        /* Close the Printer page */
  313.         if (sts=PrError()) { sprintf(tmp,"PrClosePage: ERROR %d",sts); putln(tmp); }
  314.     }
  315.     if (charh!=NULL)
  316.         HUnlock(charh);
  317. }
  318.  
  319.  
  320.  
  321. /*    printText -    Print the text selected on the screen 
  322.  *        vs - which vs to print from */
  323. void printText
  324.   (
  325.     short vs,                /* Which screen to print */
  326.     Str255 Title,        /* The title string */
  327.       short scrn
  328.   )
  329. {
  330.     char        **charh;        /* The character handle */
  331.     TPrStatus    prStatus;        /* Status record */
  332.     TPPrPort    prPort;            /* the Printer port */
  333.     THPrint        PrRecHandle;    /* our print record handle */
  334.  
  335.  
  336.     charh = RSGetTextSel(vs,0);                /* Get the characters to print */
  337.  
  338.     if ( charh==0L)
  339.         return;                                /* don't print anything.... */
  340.  
  341.     PrRecHandle = PrintSetupRecord();
  342.  
  343.     SetCursor(theCursors[normcurs]);
  344.     
  345.     if (PrJobDialog(PrRecHandle)) {            /* Cancel the print if FALSE */
  346.         prPort=PrOpenDoc(PrRecHandle,0L,0L);
  347.             if (PrError() == 0)    {
  348.                 printPages( prPort, PrRecHandle, Title, VSmaxwidth(vs), charh, (short) -1, TRUE,scrn);
  349.                 }
  350.             PrCloseDoc(prPort);
  351.  
  352.         if (((*PrRecHandle)->prJob.bJDocLoop == bSpoolLoop) && (PrError()==0))
  353.             PrPicFile(PrRecHandle,0L,0L,0L,&prStatus); /* Spool the print */
  354.         }
  355.     HPurge(charh);                            /* Kill the printed chars */
  356.     updateCursor(1);
  357.     DisposeHandle((Handle)PrRecHandle);
  358. }
  359.  
  360. void PrintPageSetup(void)
  361. {
  362.     THPrint        PrRecHandle;    /* our print record handle */
  363.     
  364.     PrOpen();
  365.  
  366.     PrRecHandle = (THPrint)GetResource('JOHN',169);                    /* AISD: JSA */
  367.     if ( PrRecHandle != NULL ) {                            /* AISD: JSA */
  368.         PrStlDialog(PrRecHandle);                    /* AISD: JSA */
  369.         ChangedResource((Handle)PrRecHandle);        /* AISD: JSA */
  370.         UpdateResFile(TelInfo->SettingsFile);        /* AISD: JSA */
  371.         }                                            /* AISD: JSA */
  372.     else {                                            /* AISD: JSA */
  373.         PrRecHandle=(THPrint)NewHandle((long)sizeof(TPrint));    /* AISD: JSA */
  374.         PrintDefault(PrRecHandle);                    /* AISD: JSA */
  375.         PrStlDialog(PrRecHandle);                    /* AISD: JSA */
  376.         UseResFile(TelInfo->SettingsFile);            /* AISD: JSA */
  377.         AddResource((Handle)PrRecHandle,'JOHN',169,"\p");    /* AISD: JSA */
  378.         UpdateResFile(TelInfo->SettingsFile);        /* AISD: JSA */
  379.         }                                            /* AISD: JSA */
  380.  
  381.     ReleaseResource((Handle)PrRecHandle);        /* AISD: JSA */
  382.     PrClose();
  383. }
  384.  
  385. void PrintSelection(void)
  386. {
  387.         short    i;
  388.  
  389.         PrOpen();
  390.             
  391.         i=RGgetdnum(FrontWindow());
  392.         if (i>-1)         
  393.             printGraph( i);                        /* Print Graphics */
  394.         else 
  395.             if ( (i=RSfindvwind(FrontWindow())) >-1 ) {
  396.                 Str255 Title;
  397.  
  398.                 GetWTitle( FrontWindow(), Title);
  399.                 printText(i, Title,scrn);    /* Print Text selection */
  400.                 }
  401.         PrClose();
  402. }
  403.  
  404. THPrint PrintSetupRecord(void)
  405. {
  406.     THPrint    PrRecHandle;
  407.     
  408.     PrRecHandle = (THPrint)GetResource('JOHN',169);                    /* AISD: JSA */
  409.  
  410.     if ( PrRecHandle != NULL ) {
  411.         PrValidate(PrRecHandle);
  412.         DetachResource((Handle)PrRecHandle);
  413.         }
  414.     else {
  415.         PrRecHandle=(THPrint)NewHandle((long)sizeof(TPrint));
  416.         PrintDefault(PrRecHandle);
  417.         }
  418.         
  419.     gPrRecHandle = PrRecHandle;
  420.     return (PrRecHandle);
  421. }
  422.